var app = getApp();
var c = require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    des: '课程非常棒，五星好评',
    hiddeninfo: true,
    starl: [1, 1, 1, 1, 1],
    starld: [1, 1, 1, 1, 1],
    classon: 1,
    _fix: 0,
    taokeinfo: null,
    taokereclass: null,
    overflow: 'hidden',
    height: '100rpx',
    shoufang: 1,
    overflowc: 'hidden',
    heightc: '100rpx',
    shoufangc: 1,
    taokelist: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {



    
    var goods_id = options.goods_id ? options.goods_id : 644;
    this.setData({
      goods_id: goods_id
    })
    var user_id = wx.getStorageSync('userInfo').user_id;
    var sqlinfo = 'w(goods_id=' + goods_id + ')f(a.video_url,a.goods_id,a.type as atype,a.video_length,a.group_time,a.starttime,money,a.discount_money,a.cate_id,b.introduce,a.number,c.name,b.type,b.teacherintroduce)jb(class|b.class_id=a.class_id)jc(user|b.uid=c.user_id)l(1)df(teacherintroduce|暂无简介)date(starttime)';
    var sqllist = 'w(topic_id=' + goods_id + ')fs(introduce|20)';
    //goods信息
    c.ajax(
      app.dbs + 'goods', {
        sql: sqlinfo
      },
      res => {
        console.log(res);
        this.setData({
          taokeinfo: res.data,
          cate_id: res.data.cate_id,
          timeout : res.data.group_time
        })
        console.log(res.data);
        var len = res.data.teacherintroduce.length;
        var lenc = res.data.introduce.length;
        // 倒计时
        this.timeout();
        if (len > 70) {
          this.setData({
            zan: 1
          })
        }
        if (lenc > 70) {
          this.setData({
            zanc: 1
          })
        }
      }
    
    )
    //状态
    c.ajax(
      app.url + 'detailstatus', {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        this.setData({
          status: res.data.status,
          // status: 1,
        })
      }

    )

  },
  timeout:function(){
    var timeout = this.data.timeout - parseInt(new Date().getTime() / 1000);
    var dd = parseInt(timeout / (24 * 3600)); //几天
    var hh = parseInt((timeout - dd * 24 * 3600) / 3600);//几个小时
    var ii = parseInt((timeout - dd * 24 * 3600 - hh * 3600) / 60);//分钟
    var ss = timeout - dd * 24 * 3600 - hh * 3600 - ii * 60;//秒
    this.setData({
      dd: dd,
      hh: hh,
      ii: ii,
      ss: ss
    })
    setTimeout(this.timeout,1000);
  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  ckickon: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      classon: id
    });
  },
  onPageScroll: function (scroll, that) {
    var topmenu = this.data.topmenu;
    if (scroll.scrollTop > topmenu) {
      this.setData({
        _fix: 1
      });
    } else {
      this.setData({
        _fix: 0
      });
    }
  },
  zanoperation: function () {
    this.setData({
      shoufang: 2,
      overflow: '',
      height: 'auto',
    })
  },
  shouoperation: function () {
    this.setData({
      shoufang: 1,
      overflow: 'hidden',
      height: '100rpx',
    })
  },
  zanoperationc: function () {
    this.setData({
      shoufangc: 2,
      overflowc: '',
      heightc: 'auto',
    })
  },
  shouoperationc: function () {
    this.setData({
      shoufangc: 1,
      overflowc: 'hidden',
      heightc: '100rpx',
    })
  },
  // 收藏
  clicksub: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id;

    var user_id = wx.getStorageSync('userInfo').user_id;
    c.ajax(
      app.url + 'addSubscript', {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        this.setData({
          subscribe: res.data,
        })

      }
    )
  },
  shopclick: function () {
    var money = this.data.taokeinfo.money;
    var goods_id = this.data.goods_id;
    wx.navigateTo({
      url: '/pages/ordersure/ordersure?goods_id=' + goods_id + '&money=' + money,
    })
  }
})